package com.huatai.bi.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.huatai.bi.dto.DirectoryDto;
import com.huatai.bi.dto.MvDirectDto;
import com.huatai.bi.dto.ReNameDirectDto;
import com.huatai.bi.dto.TransitionColumnTypeDTO;
import com.huatai.bi.entity.DirectoryEntity;
import com.huatai.bi.entity.MqMqttClientEntity;
import com.huatai.bi.query.DataProcessQuery;
import com.huatai.bi.service.DataProcessService;
import com.huatai.bi.service.DirectoryService;
import com.huatai.bi.vo.DataProcessVO;
import com.huatai.bi.vo.Direcr.DircetDataInfoVO;
import com.huatai.bi.vo.Direcr.DirectTreeVO;
import com.huatai.common.api.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.eclipse.paho.client.mqttv3.MqttClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @BelongsProject: itc-airport-cloud
 * @BelongsPackage: com.huatai.bi.controller
 * @Author: hecaiy
 * @CreateTime: 2024-06-15  09:09
 * @Description: TODO 数据集管理控制器
 * @Version: 1.0
 */
@RestController
@Api(value = "数据目录、数据集管理",tags = "数据目录")
@RequestMapping("/directory")
public class DirectoryController {
    @Autowired
    private DirectoryService directoryService;
    @Autowired
    private DataProcessService dataProcessService;

    @GetMapping("/runOneDataSet/{dtId}")
    @ApiOperation(value = "执行一次数据同步",notes = "执行一次数据同步")
    public R<Boolean> runOneDataSet(@PathVariable("dtId") Long dtId){
        return R.data(directoryService.runOneDataSet(dtId));
    }

	@PostMapping("/child/list")
	@ApiOperation(value = "通过mqttid和topic获取主终端下的子终端列表",notes = "通过mqttid和topic获取主终端下的子终端列表")
	public R<List<DirectoryEntity>> getChildList(@RequestBody MqMqttClientEntity dto){
		return directoryService.getChildList(dto);
	}

    @GetMapping("/getCronByDtId/{dtId}")
    @ApiOperation(value = "获取cron(新)",notes = "获取cron(新)")
    public R<String> getCronByDtId(@PathVariable("dtId") Long dtId){
        return R.data(directoryService.getCronByDtId(dtId));
    }

    @PostMapping("/updateCron")
    @ApiOperation(value = "更新cron(新)",notes = "更新cron(新)")
    public R<Boolean> updateCron(@RequestParam("dtId") Long dtId,@RequestParam("cron") String cron){
        return R.data(directoryService.updateCron(dtId,cron));
    }

    @PostMapping("/saveDirectory")
    @ApiOperation(value = "保存数据目录",notes = "保存数据目录")
    public R<JSONObject> saveDirectory(@RequestBody DirectoryDto dto){
        return R.data(directoryService.saveDirectory(dto));
    }

    @PostMapping("/reNameDirectory")
    @ApiOperation(value = "重命名目录",notes = "重命名目录")
    public R<Boolean> reNameDirectory(@RequestBody ReNameDirectDto dto){
        return R.data(directoryService.reNameDirectory(dto));
    }

    @GetMapping("/reMoveDirectory/{dtId}")
    @ApiOperation(value = "删除目录",notes = "删除目录")
    public R<Boolean> reMoveDirectory(@PathVariable("dtId") Long dtId){
        return R.data(directoryService.reMoveDirectory(dtId));
    }

    @PostMapping("/mvDirectory")
    @ApiOperation(value = "数据集移动目录",notes = "数据集移动目录")
    public R<Boolean> mvDirectory(@RequestBody MvDirectDto dto){
        return R.data(directoryService.mvDirectory(dto.getDtId(),dto.getPid()));
    }

    @GetMapping("/copyDataSet/{dtId}")
    @ApiOperation(value = "复制数据集",notes = "复制数据集")
    public R<Long> copyDataSet(@PathVariable("dtId") Long dtId){
        return R.data(directoryService.copyDataSet(dtId));
    }

    @GetMapping("/getDirectDataSetTree")
    @ApiOperation(value = "获取数据集树结构",notes = "获取数据集树结构")
    public R<List<DirectTreeVO>> getDirectDataSetTree( @ApiParam(name = "name",value = "场景版本号",required = false) @RequestParam(name = "name") String name){
        return R.data(directoryService.getDirectDataSetTree(name));
    }

	@GetMapping("/getDirectDataSetTreeByClientId")
	@ApiOperation(value = "根据客户端ID获取数据集树结构",notes = "根据客户端ID获取数据集树结构")
	public R<List<DirectTreeVO>> getDirectDataSetTreeByClientId(@RequestParam(name = "clientId") String clientId){
		return R.data(directoryService.getDirectDataSetTreeByClientId(clientId));
	}

    @GetMapping("/getDirectTree")
    @ApiOperation(value = "获取目录树",notes = "获取目录树")
    public R<List<DirectTreeVO>> getDirectTree(){
        return R.data(directoryService.getDirectTree());
    }

    @GetMapping("/removeById/{id}")
    @ApiOperation(value = "移除数据加工数据集",notes = "移除数据加工数据集")
    public R<Boolean> removeById(@PathVariable(value = "id") Long id){
        return R.data(dataProcessService.removeById(id));
    }

    @PostMapping("/getPageData")
    @ApiOperation(value = "分页获取数据加工",notes = "分页获取数据加工")
    public R<IPage<DataProcessVO>> getPageData(@RequestBody DataProcessQuery query){
        return R.data(dataProcessService.getPageData(query.getId(),query.getPageIndex(),query.getPageSize()));
    }

    @GetMapping("/getDircetDataInfo/{dtId}")
    @ApiOperation(value = "获取数据集详情",notes = "获取数据集详情")
    public R<DircetDataInfoVO> getDircetDataInfo(@PathVariable(value = "dtId") Long dtId){
        return R.data(directoryService.getDircetDataInfo(dtId));
    }

    @PostMapping("/isTransitionColumType")
    @ApiOperation(value = "判断字段类型是否是转换",notes = "判断字段类型是否是转换")
    public R<Boolean> isTransitionColumType(@RequestBody TransitionColumnTypeDTO dto){
        return R.data(directoryService.isTransitionColumType(dto.getId(),dto.getTargetType()));
    }
}
